﻿package org.flixel
{
    import flash.display.*;
    import org.flixel.plugin.*;

    public class FlxPath extends Object
    {
        public var nodes:Array;
        public var debugColor:uint;
        public var debugScrollFactor:FlxPoint;
        public var ignoreDrawDebug:Boolean;
        protected var _point:FlxPoint;

        public function FlxPath(param1:Array = null)
        {
            if (param1 == null)
            {
                this.nodes = new Array();
            }
            else
            {
                this.nodes = param1;
            }
            this._point = new FlxPoint();
            this.debugScrollFactor = new FlxPoint(1, 1);
            this.debugColor = 16777215;
            this.ignoreDrawDebug = false;
            var _loc_2:* = manager;
            if (_loc_2 != null)
            {
                _loc_2.add(this);
            }
            return;
        }// end function

        public function destroy() : void
        {
            var _loc_1:* = manager;
            if (_loc_1 != null)
            {
                _loc_1.remove(this);
            }
            this.debugScrollFactor = null;
            this._point = null;
            this.nodes = null;
            return;
        }// end function

        public function add(param1:Number, param2:Number) : void
        {
            this.nodes.push(new FlxPoint(param1, param2));
            return;
        }// end function

        public function addAt(param1:Number, param2:Number, param3:uint) : void
        {
            if (param3 > this.nodes.length)
            {
                param3 = this.nodes.length;
            }
            this.nodes.splice(param3, 0, new FlxPoint(param1, param2));
            return;
        }// end function

        public function addPoint(param1:FlxPoint, param2:Boolean = false) : void
        {
            if (param2)
            {
                this.nodes.push(param1);
            }
            else
            {
                this.nodes.push(new FlxPoint(param1.x, param1.y));
            }
            return;
        }// end function

        public function addPointAt(param1:FlxPoint, param2:uint, param3:Boolean = false) : void
        {
            if (param2 > this.nodes.length)
            {
                param2 = this.nodes.length;
            }
            if (param3)
            {
                this.nodes.splice(param2, 0, param1);
            }
            else
            {
                this.nodes.splice(param2, 0, new FlxPoint(param1.x, param1.y));
            }
            return;
        }// end function

        public function remove(param1:FlxPoint) : FlxPoint
        {
            var _loc_2:* = this.nodes.indexOf(param1);
            if (_loc_2 >= 0)
            {
                return this.nodes.splice(_loc_2, 1)[0];
            }
            return null;
        }// end function

        public function removeAt(param1:uint) : FlxPoint
        {
            if (this.nodes.length <= 0)
            {
                return null;
            }
            if (param1 >= this.nodes.length)
            {
                param1 = this.nodes.length - 1;
            }
            return this.nodes.splice(param1, 1)[0];
        }// end function

        public function head() : FlxPoint
        {
            if (this.nodes.length > 0)
            {
                return this.nodes[0];
            }
            return null;
        }// end function

        public function tail() : FlxPoint
        {
            if (this.nodes.length > 0)
            {
                return this.nodes[(this.nodes.length - 1)];
            }
            return null;
        }// end function

        public function drawDebug(param1:FlxCamera = null) : void
        {
            var _loc_3:FlxPoint = null;
            var _loc_4:FlxPoint = null;
            var _loc_7:uint = 0;
            var _loc_8:uint = 0;
            var _loc_9:Number = NaN;
            if (this.nodes.length <= 0)
            {
                return;
            }
            if (param1 == null)
            {
                param1 = FlxG.camera;
            }
            var _loc_2:* = FlxG.flashGfx;
            _loc_2.clear();
            var _loc_5:uint = 0;
            var _loc_6:* = this.nodes.length;
            while (_loc_5 < _loc_6)
            {
                
                _loc_3 = this.nodes[_loc_5] as FlxPoint;
                this._point.x = _loc_3.x - int(param1.scroll.x * this.debugScrollFactor.x);
                this._point.y = _loc_3.y - int(param1.scroll.y * this.debugScrollFactor.y);
                this._point.x = int(this._point.x + (this._point.x > 0 ? (1e-007) : (-1e-007)));
                this._point.y = int(this._point.y + (this._point.y > 0 ? (1e-007) : (-1e-007)));
                _loc_7 = 2;
                if (_loc_5 == 0 || _loc_5 == (_loc_6 - 1))
                {
                    _loc_7 = _loc_7 * 2;
                }
                _loc_8 = this.debugColor;
                if (_loc_6 > 1)
                {
                    if (_loc_5 == 0)
                    {
                        _loc_8 = FlxG.GREEN;
                    }
                    else if (_loc_5 == (_loc_6 - 1))
                    {
                        _loc_8 = FlxG.RED;
                    }
                }
                _loc_2.beginFill(_loc_8, 0.5);
                _loc_2.lineStyle();
                _loc_2.drawRect(this._point.x - _loc_7 * 0.5, this._point.y - _loc_7 * 0.5, _loc_7, _loc_7);
                _loc_2.endFill();
                _loc_9 = 0.3;
                if (_loc_5 < (_loc_6 - 1))
                {
                    _loc_4 = this.nodes[(_loc_5 + 1)];
                }
                else
                {
                    _loc_4 = this.nodes[0];
                    _loc_9 = 0.15;
                }
                _loc_2.moveTo(this._point.x, this._point.y);
                _loc_2.lineStyle(1, this.debugColor, _loc_9);
                this._point.x = _loc_4.x - int(param1.scroll.x * this.debugScrollFactor.x);
                this._point.y = _loc_4.y - int(param1.scroll.y * this.debugScrollFactor.y);
                this._point.x = int(this._point.x + (this._point.x > 0 ? (1e-007) : (-1e-007)));
                this._point.y = int(this._point.y + (this._point.y > 0 ? (1e-007) : (-1e-007)));
                _loc_2.lineTo(this._point.x, this._point.y);
                _loc_5 = _loc_5 + 1;
            }
            param1.buffer.draw(FlxG.flashGfxSprite);
            return;
        }// end function

        public static function get manager() : DebugPathDisplay
        {
            return FlxG.getPlugin(DebugPathDisplay) as DebugPathDisplay;
        }// end function

    }
}
